package com.yks.rpc;

import com.yks.rpc.transport.TransportClient;
import org.apache.commons.io.IOUtils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class GreetingClient2 {
    public static void main(String[] args) {
        try {
            String url = "http://127.0.0.1:3000";
            HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(url).openConnection();
            //输出数据
            httpURLConnection.setDoOutput(true);
            //读数据
            httpURLConnection.setDoInput(true);
            //不要缓存
            httpURLConnection.setUseCaches(false);
            httpURLConnection.setRequestMethod("POST");

            httpURLConnection.connect();
            IOUtils.copy(new ByteArrayInputStream("client:hello".getBytes()), httpURLConnection.getOutputStream());
            int responseCode = httpURLConnection.getResponseCode();

            InputStream inputStream;
            if (responseCode == HttpURLConnection.HTTP_OK) {
                inputStream = httpURLConnection.getInputStream();
            } else {
                inputStream = httpURLConnection.getErrorStream();
            }
            byte[] bytes = IOUtils.readFully(inputStream, inputStream.available());
            System.out.println(new String(bytes));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
